package com.csj.mvvmtest.util;

import android.annotation.SuppressLint;
import android.content.Context;
import android.provider.Settings;

import com.csj.mvvm.utils.LogUtil;
import com.csj.mvvmtest.BaseApp;
import com.google.android.gms.ads.identifier.AdvertisingIdClient;
import com.google.android.gms.common.GooglePlayServicesNotAvailableException;

import java.io.IOException;

public class IDUtil {

    public static String getGAID() {
        String gaid = "";
        AdvertisingIdClient.Info adInfo = null;
        try {
            adInfo = AdvertisingIdClient.getAdvertisingIdInfo(BaseApp.instance);
        } catch (IOException e) {
            // Unrecoverable error connecting to Google Play services (e.g.,
            // the old version of the service doesn't support getting AdvertisingId).
            LogUtil.e("IOException");
        } catch (GooglePlayServicesNotAvailableException e) {
            // Google Play services is not available entirely.
            LogUtil.e("GooglePlayServicesNotAvailableException");
        } catch (Exception e) {
            LogUtil.e("Exception:" + e.toString());
            // Encountered a recoverable error connecting to Google Play services.
        }
        if (adInfo != null) {
            gaid = adInfo.getId();
            LogUtil.d("gaid:" + gaid);
        }
        return gaid;
    }

    public static String getAndroidId() {
        try {
            @SuppressLint("HardwareIds")
            String androidId = Settings.Secure.getString(BaseApp.instance.getContentResolver(),
                    Settings.Secure.ANDROID_ID);
            LogUtil.d("getAndroidId:" + androidId, "getAndroidId");
            return androidId;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }
}
